package com.cx.core.rocketmq.service;

import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

@Service
public class MessageService {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    public void sendMessage(String topic, String message) {
        rocketMQTemplate.convertAndSend(topic, message);
    }

    /**
     * 异步发送消息
     * @param topic
     * @param message
     * @param sendCallback
     * @author zhangcx
     * @create $date$ $time$     */
    public void asyncSend(String topic, String message, SendCallback sendCallback) {
        rocketMQTemplate.asyncSend(topic, message, sendCallback);
    }

    /**
     * 延迟消息发送
     * @param topic
     * @param message
     * @param timeout
     * @param level
     * @author zhangcx
     * @create $date$ $time$     */
    public void sendMessage(String topic, String message, int timeout, int level) {
        rocketMQTemplate.syncSend(topic, MessageBuilder.withPayload(message).build(), timeout, level);
    }

    /**
     * 消息顺序
     * @param topic
     * @param message
     * @param hashKey
     * @author zhangcx
     * @create $date$ $time$     */
    public void syncSendOrderly(String topic, String message, String hashKey) {
        rocketMQTemplate.syncSendOrderly(topic, message, hashKey);
    }
}
